//    Copyright (C) Kherty.  All rights reserved.
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using OpenLS.Spreadsheet.UI.Internals;
using OpenLS.Spreadsheet.UI.Internals.Resources;

namespace OpenLS.Spreadsheet.UI
{
    internal static class GridProtection
    {
        internal static void EnableCommands(UIElement grid)
        {
            grid.CommandBindings.Add(new CommandBinding(SpreadsheetCommands.ProtectSheet, OnProtectWorksheet,
                                                        OnHasNoProtection));
            grid.CommandBindings.Add(new CommandBinding(SpreadsheetCommands.UnprotectSheet, OnUnprotectWorksheet,
                                                        OnHasProtection));
            grid.CommandBindings.Add(new CommandBinding(SpreadsheetCommands.ProtectWorkbook, OnProtectWorkbook,
                                                        OnHasNoWorkbookProtection));
            grid.CommandBindings.Add(new CommandBinding(SpreadsheetCommands.UnprotectWorkbook, OnUnprotectWorkbook,
                                                        OnHasWorkbookProtection));
        }

        private static void OnUnprotectWorkbook(object sender, ExecutedRoutedEventArgs e)
        {
            Workbook workbook = SpreadsheetElement.GetWorkbook((DependencyObject) sender);
            if (string.IsNullOrEmpty(workbook.Protection.PasswordHash))
            {
                workbook.Protection = null;
            }
            else
            {
                var vm = new WorkbookProtectionViewModel(workbook);
                bool b = PropertyHelper.ShowPropertiesNonHostedFor(sender as FrameworkElement,
                                                                   UIStringTable.ProtectStructureAndWindows, vm,
                                                                   new WorkbookProtectionResources());
                if (b)
                {
                    if (!vm.CanUnprotect)
                        MessageBox.Show(UIStringTable.InvalidPassword, "Spreadsheet");
                    else
                        vm.Unprotect();
                }
            }
            e.Handled = true;
        }

        private static void OnHasWorkbookProtection(object sender, CanExecuteRoutedEventArgs e)
        {
            Workbook workbook = SpreadsheetElement.GetWorkbook((DependencyObject) sender);
            e.CanExecute = workbook.Protection != null;
            e.Handled = true;
        }

        private static void OnHasNoWorkbookProtection(object sender, CanExecuteRoutedEventArgs e)
        {
            Workbook workbook = SpreadsheetElement.GetWorkbook((DependencyObject) sender);
            e.CanExecute = workbook.Protection == null;
            e.Handled = true;
        }

        private static void OnProtectWorkbook(object sender, ExecutedRoutedEventArgs e)
        {
            Workbook workbook = SpreadsheetElement.GetWorkbook((DependencyObject) sender);
            var vm = new WorkbookProtectionViewModel(workbook);
            bool b = PropertyHelper.ShowPropertiesNonHostedFor(sender as FrameworkElement,
                                                               UIStringTable.ProtectStructureAndWindows, vm,
                                                               new WorkbookProtectionResources());
            if (b)
            {
                vm.Protect();
            }
            e.Handled = true;
        }

        private static void OnProtectWorksheet(object sender, ExecutedRoutedEventArgs e)
        {
            Worksheet ws = SpreadsheetElement.GetWorksheet(sender);
            var vm = new WorksheetProtectionViewModel(ws);
            bool b = PropertyHelper.ShowPropertiesNonHostedFor(sender as FrameworkElement,
                                                               UIStringTable.ProtectSheet, vm,
                                                               new WorksheetProtectionResources());
            if (b)
            {
                vm.Protect();
            }
            e.Handled = true;
        }

        private static void OnHasNoProtection(object sender, CanExecuteRoutedEventArgs e)
        {
            Worksheet ws = SpreadsheetElement.GetWorksheet(sender);
            e.CanExecute = ws != null && ws.Protection == null;
            e.Handled = true;
        }

        private static void OnHasProtection(object sender, CanExecuteRoutedEventArgs e)
        {
            Worksheet ws = SpreadsheetElement.GetWorksheet(sender);
            e.CanExecute = ws != null && ws.Protection != null;
            e.Handled = true;
        }

        private static void OnUnprotectWorksheet(object sender, ExecutedRoutedEventArgs e)
        {
            Worksheet ws = SpreadsheetElement.GetWorksheet(sender);
            if (string.IsNullOrEmpty(ws.Protection.PasswordHash))
            {
                ws.Protection = null;
            }
            else
            {
                var vm = new WorksheetProtectionViewModel(ws);
                bool b = PropertyHelper.ShowPropertiesNonHostedFor(sender as FrameworkElement,
                                                                   UIStringTable.ProtectSheet, vm,
                                                                   new WorksheetProtectionResources());
                if (b)
                {
                    if (!vm.CanUnprotect)
                        MessageBox.Show(UIStringTable.InvalidPassword, "Spreadsheet");
                    else
                        vm.Unprotect();
                }
            }
            e.Handled = true;
        }
    }
}